package com.mti.app.view
{
	import com.mti.app.ApplicationFacade;
	import com.mti.app.model.business.QRBuilder;
	import com.mti.app.view.mycomponents.InfosQRCodeView;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.net.FileFilter;
	import flash.utils.ByteArray;
	
	import media.MediaPicture;
	import media.MediaPoint;
	import media.MediaText;
	
	import mx.collections.ArrayCollection;
	
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	public class InfosQRCodeViewMediator extends Mediator
	{
		public static const NAME:String = "InfosQRCodeViewMediator";
		
		private var _mediaPoint:MediaPoint = null;
		[Bindable]
		public function get mediaPoint():MediaPoint
		{
			return _mediaPoint;
		}
		public function set mediaPoint(value:MediaPoint):void
		{
			_mediaPoint = value;
			if (value != null)
				generateQrCode();
		}
		
		public function InfosQRCodeViewMediator(viewComponent:InfosQRCodeView)
		{
			super(NAME, viewComponent);
			
			view.mediator = this;
			
			view.mapContainer.addEventListener(MouseEvent.CLICK, mapContainerClickHandler);
			view.qrcodeImageButton.addEventListener(MouseEvent.CLICK, qrcodeImageClick);
			view.addTextButton.addEventListener(MouseEvent.CLICK, addTextButtonClickHandler);
			view.deleteTextButton.addEventListener(MouseEvent.CLICK, deleteTextButtonClickHandler);
			view.addImageButton.addEventListener(MouseEvent.CLICK, addImageButtonClickHandler);
			view.deleteImageButton.addEventListener(MouseEvent.CLICK, deleteImageButtonClickHandler);
		}
		
		private function get view():InfosQRCodeView
		{
			return viewComponent as InfosQRCodeView;
		}
		
		private function generateQrCode():void
		{
			view.qrcodeImage.source = QRBuilder.createQr(mediaPoint.QRCode, 200);
		}
		
		private function qrcodeImageClick(event:MouseEvent):void
		{
			QRBuilder.saveQR(view.qrcodeImage);
		}
		
		override public function listNotificationInterests():Array
		{
			return [ApplicationFacade.LOADING_INFOSQRCODE_COMPLETE];
		}
		
		private var file:File = null;
		
		private function addTextButtonClickHandler(event:MouseEvent):void
		{
			file = new File();
			var filter:FileFilter = new FileFilter("Texte (*.txt)", "*.txt");
			file.addEventListener(Event.SELECT, textFileSelectHandler);
			file.browseForOpen("Sélectionnez un fichier texte", [filter]);
		}
		
		private function textFileSelectHandler(event:Event):void
		{
			file.removeEventListener(Event.SELECT, textFileSelectHandler);
			var fileStream:FileStream = new FileStream();
			fileStream.open(file, FileMode.READ);
			
			var mediaText:MediaText = new MediaText();
			mediaText.name = file.name;
			mediaText.title = file.name;
			mediaText.point = mediaPoint;
			mediaText.text = fileStream.readUTFBytes(fileStream.bytesAvailable);
			
			fileStream.close();
			mediaPoint.textList.addItem(mediaText);
		}
		
		private function deleteTextButtonClickHandler(event:MouseEvent):void
		{
			mediaPoint.textList.removeItemAt(mediaPoint.textList.getItemIndex(view.textList.selectedItem));
			mediaPoint.deletedTextList.addItem(view.textList.selectedItem);
		}
		
		private function addImageButtonClickHandler(event:MouseEvent):void
		{
			file = new File();
			var filter:FileFilter = new FileFilter("Image (*.jpg)", "*.jpg");
			file.addEventListener(Event.SELECT, imageFileSelectHandler);
			file.browseForOpen("Sélectionnez une image", [filter]);
		}
		
		private function imageFileSelectHandler(event:Event):void
		{
			file.removeEventListener(Event.SELECT, imageFileSelectHandler);
			var fileStream:FileStream = new FileStream();
			fileStream.open(file, FileMode.READ);
			
			var picture:MediaPicture = new MediaPicture();
			picture.name = file.name;
			picture.title = file.name;
			picture.point = mediaPoint;
			picture.imagePath = file.name;
			picture.imageData = new ByteArray();
			fileStream.readBytes(picture.imageData, 0, fileStream.bytesAvailable);
			
			fileStream.close();
			if (mediaPoint.pictureList == null)
				mediaPoint.pictureList = new ArrayCollection();
			mediaPoint.pictureList.addItem(picture);
		}
		
		private function deleteImageButtonClickHandler(event:MouseEvent):void
		{
			mediaPoint.pictureList.removeItemAt(mediaPoint.pictureList.getItemIndex(view.pictureList.selectedItem));
			mediaPoint.deletedPictureList.addItem(view.pictureList.selectedItem);
		}
		
		private function mapContainerClickHandler(event:MouseEvent):void
		{
			mediaPoint.posX = view.mapContainer.mouseX - view.pointImage.width / 2;
			mediaPoint.posY = view.mapContainer.mouseY - view.pointImage.height / 2;
		}
	}
}